import { getContractCheck,getContractPass } from "../../../network/client"
import { debounce } from '../../../utils/Tools'

// pages/client/contract-status/contract-status.js
Page({
  data: {
    id:null,
    code: null,
    require:null,
    supply:null,
    active: 1,
    Signed:'',
    Unsigned:'',
    passContract:[],
    checkContract:[],
    passAfter:[],
    checkAfter:[],
    status:{
      '0':'审核中',"1":"已通过",'2':'未通过'
    },
    sele:'1',
    isSeleContract: false
  },
  onLoad: function (options) {
    this.handleFilter = debounce(this.handleFilter,300)
  },
  onShow(){
    //   调用网络请求
    this._getContractStatus()
  },
  //      函数事件        //
  handleTabs(e){  //   处理切换导航选项
    const {index} = e.detail
    this.setData({
      Signed:'',
      Unsigned:''
    })
    switch (index) {
      case 0:
        this.setData({passAfter:this.data.passContract})
        break;
      case 1:
        this.setData({checkAfter:this.data.checkContract})        
        break;
    }
  },
  async handleFilter(e){
    const {type} = e.currentTarget.dataset
    switch(type){
      case 'sign':
        const result = this.data.passContract.filter(res =>{
          return res.agreement_code.includes(e.detail)
        })
        if(!result) this.setData({passAfter: this.data.passContract})
        this.setData({passAfter:result})
        break;
      case 'unsign':
        const res = this.data.checkContract.filter(res =>{
          return res.customer_name.includes(e.detail)
        })
        if(!res) this.setData({checkAfter: this.data.checkContract})
        this.setData({checkAfter:res})
        break;
    }
  },
  handleSeleContract(e){ //处理点击去签合同打开选项
    const {id,code,require,supply} = e.currentTarget.dataset
    this.setData({
      isSeleContract:true,
      id,code,require,supply
    })
  },
  handleRadio(e){
    this.setData({
      sele: e.detail,
      isSeleContract: false
    });
    if(this.data.sele === '1'){
      wx.navigateTo({
        url: `/pages/client/client-contract/client-contract?agreement_id=${this.data.id}&agreement_code=${this.data.code}` 
      });
    }else{
      wx.navigateTo({
        url: `/pages/client/upload-contract/upload-contract?agreement_id=${this.data.id}&agreement_code=${this.data.code}&supply=${this.data.supply}&require=${this.data.require}` 
      });
    }
  },
  onClose(){
    this.setData({
      isSeleContract: false
    });
  },
  //      网络请求      //
  async _getContractStatus(){
    const pass = await getContractPass()
    const check = await getContractCheck()
    if(pass.code === 200 || check.code === 200){
      this.setData({
        passContract: pass.data.agreement,
        checkContract: check.data.agreement,
        passAfter:pass.data.agreement,
        checkAfter: check.data.agreement
      })
    }
  }
})